package com.itheima.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberMapper;
import com.itheima.dao.OrderMapper;
import com.itheima.dao.OrderSettingMapper;
import com.itheima.dao.SetmealMapper;
import com.itheima.entity.Result;
import com.itheima.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: tantan
 * @date: 2020/4/20-8:47
 * @Version: 1.0.0
 */
@Service(interfaceClass = AppointmentService.class)
@Transactional
public class AppointmentServiceImpl implements AppointmentService {
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    OrderSettingMapper orderSettingMapper;
    @Autowired
    MemberMapper memberMapper;
    @Autowired
    OrderMapper orderMapper;
    @Override
    public List<Setmeal> findAllSetmeals() {
        return setmealMapper.findAll();
    }

    @Override
    public Result submit(OrderInfo orderInfo) {
        Date orderDate = orderInfo.getOrderDate();
        String orderDateStr = new DateTime(orderDate).toString("yyyy-MM-dd");
        orderDate = DateUtil.parse(orderDateStr,"yyyy-MM-dd");
        String telephone = orderInfo.getTelephone();
        String name = orderInfo.getName();
        String idCard = orderInfo.getIdCard();
        Integer sex = orderInfo.getSex();
        String orderType = orderInfo.getOrderType();
        Integer setmealId = orderInfo.getSetmealId();
        OrderSetting orderSetting = orderSettingMapper.findByDate(orderDate);
        if (orderSetting == null) {
            return Result.ERROR("不开放预约，请另选时间");
        }
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if ( number <= reservations) {
            return Result.ERROR("当前时间已约满，请另选时间");
        }

        //判断是否是会员
        Member member = memberMapper.queryMemberBy(telephone);
        if (member == null) {
            member = new Member();
            member.setName(name);
            member.setSex(String.valueOf(sex));
            member.setIdCard(idCard);
            member.setPhoneNumber(telephone);
            member.setRegTime(new Date());
            memberMapper.add(member);
        }
        //查询是否重复预约
        Integer memberId = member.getId();
        Order condition = new Order();
        condition.setMemberId(memberId);
        condition.setSetmealId(setmealId);
        condition.setOrderDate(orderDate);
        List<Order> orders =  orderMapper.queryByCondition(condition);
        if (CollectionUtil.isNotEmpty(orders)) {
            return Result.ERROR("请勿重复预约");
        }
        //下单
        Order order = new Order();
        order.setMemberId(memberId);
        order.setOrderDate(orderDate);
        order.setOrderType(orderType);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(setmealId);
        orderMapper.add(order);

        int rows = orderSettingMapper.updateReservations(orderSetting.getId(),orderSetting.getVersion());
        if (rows == 0) {
            throw new OptimisticLockingFailureException("更新失败");
        }

        return Result.OK("预约成功",order);
    }

}
